﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WAContratos.Manager;
using WebContratos.Bean;
using System.Collections.Generic;

namespace WebContratos.reportes
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        cls_negocios neg = new cls_negocios();
        public String basePath = "../";
        public String departamentoSe = "STM";
        public String rutaactivado = "";
        public String codContrato = "";
        public String ruta = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            
           

            if (!(IsPostBack))
            {
                int anio = Convert.ToInt32(System.DateTime.Now.ToString("yyyy"));
                ListItem uItem = new ListItem();


                for (int a = anio - 10; a <= anio; a++)
                {
                    uItem.Text = Convert.ToString(a);
                    uItem.Value = Convert.ToString(a);
                    uItem = new ListItem();
                    this.dpListaAnios.Items.Add(uItem);

                    if (a == anio)
                    {
                        this.dpListaAnios.SelectedValue = Convert.ToString(a);
                    }
                }

                consultaGrid("2011");
            }
          
        }

        private void consultaGrid(string anio)
        {
            DataSet dt = new DataSet();
            String sq = "";

            sq = "select gerencia_solicitante, count(1) cantidad, sum(nvl(f.monto_contrato,0)) valor from t_contrato f WHERE GERENCIA_SOLICITANTE IS not NULL AND desgerencia_solicitante is not null and to_char(f.fecha_suscripcion,'rrrr') = '" + anio + "' group by gerencia_solicitante";

            dt = neg.dtset(sq);

            gerenciasTotalBean uGeren = new gerenciasTotalBean();
            List<gerenciasTotalBean> allGeren = new List<gerenciasTotalBean>();
            ContratoManager uMana = new ContratoManager();
            string descrGere = "";

            foreach (DataRow row in dt.Tables[0].Rows)
            {
                uGeren.Area = Convert.ToString(row["gerencia_solicitante"]);

                descrGere = uMana.obtieneDescripcion(uGeren.Area);

                uGeren.Area = uGeren.Area + " - " + descrGere;

                uGeren.Cantidad = Convert.ToInt32(row["cantidad"]);
                uGeren.Valor = Convert.ToDouble(row["valor"]);

                allGeren.Add(uGeren);
                uGeren = new gerenciasTotalBean();                
            }

            grdcontratos.DataSource = allGeren;
            grdcontratos.DataBind();

        }

        protected void btConsulta_Click(object sender, EventArgs e)
        {
            consultaGrid(this.dpListaAnios.SelectedValue);
        }
    }
}
